package cn.fintecher.pangolin.service.business.model.request;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Objects;


/**
 * @Auther: xiaqun
 * @Description: 查询其他催记请求参数
 * @Date: 16:25 2019/9/17
 */

@Data
public class FindOtherRecordRequest {
    @ApiModelProperty(value = "证件号")
    @NotNull(message = "personal.is.required")
    private String certificateNo;

    @ApiModelProperty(value = "方式")
    private String findWay;

    @ApiModelProperty(value = "最小查找时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date minFindTime;

    @ApiModelProperty(value = "最大查找时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date maxFindTime;

    public BoolQueryBuilder getBuilder() {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        builder.must(QueryBuilders.matchPhraseQuery("flag", 0));
        builder.must(QueryBuilders.matchPhraseQuery("certificateNo.keyword", this.certificateNo));
        if (Objects.nonNull(this.minFindTime)) {
            builder.must(QueryBuilders.rangeQuery("operateTime").gte(this.minFindTime.getTime()));
        }
        if (Objects.nonNull(this.maxFindTime)) {
            builder.must(QueryBuilders.rangeQuery("operateTime").lte(this.maxFindTime.getTime() + 86400000));
        }
        return builder;
    }
}
